﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IAirPlanePlugin;
using HtmlAgilityPack;

namespace VietNamAirLineFeeder
{

    public class VietNamAirPlaneFeeder:IAirPlaneFeeder
    {
        public List<AirPlaneTicketInfo> getTicketInfo(string NoiDi, string NoiDen, string NgayDi, string ThangDi,int idCuaHang)
        {
            Dictionary<string, string> monthParse = new Dictionary<string, string>();
            monthParse.Add("1","JAN");
            monthParse.Add("2", "FEB");
            monthParse.Add("3", "MAR");
            monthParse.Add("4", "APR");
            monthParse.Add("5", "MAY");
            monthParse.Add("6", "JUN");
            monthParse.Add("7", "JUL");
            monthParse.Add("8", "AUG");
            monthParse.Add("9", "SEP");
            monthParse.Add("10", "OCT");
            monthParse.Add("11", "NOV");
            monthParse.Add("12", "DEC");

            ThangDi = monthParse[ThangDi];
            

            var webGet = new HtmlWeb();
            List<AirPlaneTicketInfo> listTickets = new List<AirPlaneTicketInfo>();
            string url = "https://cat.sabresonicweb.com/SSWVN/meridia?language=vi&posid=B3QE&page=requestAirMessage_air&action=airRequest&realrequestAir=realRequestAir&actionType=nonFlex&classService=CoachClass&currency=VND&depTime=0600&retTime=0600&direction=returntravel&depDay="+NgayDi+"&depMonth="+ThangDi+"&temp_date=&retDay="+NgayDi+"&retMonth="+ThangDi+"&temp_date=&ADT=1&CHD=0&wclang=VI&returnCity=SGN&departCity=BMV";
            var document = webGet.Load(url);
            if (document != null)
            {
                HtmlNodeCollection Nodes = document.DocumentNode.SelectNodes("//*[@id='bfm_tbl_out']/tbody/tr");
                if (Nodes != null && Nodes.Count > 0)
                {
                    for (int i = 0; i < Nodes.Count; i++)
                    {
                        VietNameAirPlaneTicketInfo ticket = new VietNameAirPlaneTicketInfo();
                        ticket.HangMayBay = "VietNamAirLine";
                        HtmlNode TdGioDi = Nodes[i].SelectSingleNode("child::td[@class='matrix_cal_body matrix_cal_body_dep']/div/p");
                        if (TdGioDi!=null && TdGioDi.InnerText != null)
                            ticket.GioDi = TdGioDi.InnerText;

                        HtmlNode TdGioDen = Nodes[i].SelectSingleNode("child::td[@class='matrix_cal_body matrix_cal_body_arr']/div/p");
                        if (TdGioDen!=null && TdGioDen.InnerText != null)
                            ticket.GioDen = TdGioDen.InnerText;                     

                        HtmlNode TdPhoThongLinhHoat = Nodes[i].SelectSingleNode("child::td[@class='matrix_cal_body matrix_cal_body_fare3 fare_EF']/span/a");
                        if (TdPhoThongLinhHoat != null && TdPhoThongLinhHoat.InnerText != null)
                            ticket.PhoThongLinhHoat = TdPhoThongLinhHoat.InnerText.Trim();

                        HtmlNode TdTietKiemLinhHoat = Nodes[i].SelectSingleNode("child::td[@class='matrix_cal_body matrix_cal_body_fare5 fare_SF']/span/a");
                        if (TdTietKiemLinhHoat != null && TdTietKiemLinhHoat.InnerText != null)
                            ticket.TietKiemLinhHoat = TdTietKiemLinhHoat.InnerText.Trim();

                        HtmlNode TdTietKiem = Nodes[i].SelectSingleNode("child::td[@class='matrix_cal_body matrix_cal_body_fare6 fare_ES']/span/a");
                        if (TdTietKiem != null && TdTietKiem.InnerText != null)
                            ticket.TietKiem = TdTietKiem.InnerText.Trim();

                        HtmlNode TdSieuTietKiemTietKiem = Nodes[i].SelectSingleNode("child::td[@class='matrix_cal_body matrix_cal_body_fare7 fare_SS']/span/a");
                        if (TdSieuTietKiemTietKiem != null && TdSieuTietKiemTietKiem.InnerText != null)
                            ticket.SieuTietKiem = TdSieuTietKiemTietKiem.InnerText.Trim();

                        if (idCuaHang == -1)
                            listTickets.Add(ticket.FormatOutput());
                        else
                            listTickets.Add(ticket);
                    }
                }
            }

            return listTickets;
        }

        public string GetName()
        {
            return "Việt Nam AirLine";
        }

        public List<string> getListColumnName()
        {
            List<string> listColumn = new List<string>();
            listColumn.Add("GioDi");
            listColumn.Add("GioDen");
            listColumn.Add("HangMayBay");
            listColumn.Add("PhoThongLinhHoat");
            listColumn.Add("TietKiemLinhHoat");
            listColumn.Add("TietKiem");
            listColumn.Add("SieuTietKiem");
            return listColumn;
        }

        public class VietNameAirPlaneTicketInfo : AirPlaneTicketInfo
        {
            private string _PhoThongLinhHoat;

            public string PhoThongLinhHoat
            {
                get { return _PhoThongLinhHoat; }
                set { _PhoThongLinhHoat = value; }
            }
            private string _TietKiemLinhHoat;

            public string TietKiemLinhHoat
            {
                get { return _TietKiemLinhHoat; }
                set { _TietKiemLinhHoat = value; }
            }
            private string _TietKiem;

            public string TietKiem
            {
                get { return _TietKiem; }
                set { _TietKiem = value; }
            }
            private string _SieuTietKiem;

            public string SieuTietKiem
            {
                get { return _SieuTietKiem; }
                set { _SieuTietKiem = value; }
            }

            public override AirPlaneTicketInfo FormatOutput()
            {
                AirPlaneTicketInfo Result = new AirPlaneTicketInfo();
                Result.GioDi = GioDi;
                Result.GioDen = GioDen;
                Result.HangMayBay = HangMayBay;
                Result.GiaVeCacLoai += "Phổ thông linh hoạt: " + PhoThongLinhHoat + "\n"
                                    + "Tiết kiệm linh hoạt: " + TietKiemLinhHoat + "\n"
                                    + "Tiết kiệm: " + TietKiem + "\n"
                                    + "Siêu tiết kiệm: " + SieuTietKiem;
                return Result;
            }

            public override string getAttributeValue(string attName)
            {   
                if (attName.Equals("HangMayBay"))
                {
                    return HangMayBay;
                }
                else if (attName.Equals("PhoThongLinhHoat"))
                {
                    return PhoThongLinhHoat;
                }
                else if (attName.Equals("TietKiemLinhHoat"))
                {
                    return TietKiemLinhHoat;
                }
                else if (attName.Equals("TietKiem"))
                {
                    return TietKiem;
                }
                else if (attName.Equals("SieuTietKiem"))
                {
                    return SieuTietKiem;
                }
                else if (attName.Equals("GioDi"))
                {
                    return GioDi;
                }
                else if (attName.Equals("GioDen"))
                {
                    return GioDen;
                }
                return "";
            }
        }
    }
}
